package yoyo.secret.app.model;

import java.util.List;

import yoyo.secret.app.R;
import yoyo.secret.app.db.DataBaseOperation;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class ClassificationAdapter extends BaseAdapter {

	private List<ClassIfication> ifications;
	private Context mContext;
	private DataBaseOperation mDataBaseOperation;

	public ClassificationAdapter(List<ClassIfication> data, Context context, DataBaseOperation dataBaseOperation) {
		ifications = data;
		mContext = context;
		mDataBaseOperation = dataBaseOperation;
	}

	public void addData(ClassIfication classIfication) {
		ifications.add(classIfication);
		notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return ifications.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return ifications.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ClassIfication classIfication = ifications.get(position);
		ViewHolder holder = null;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = LayoutInflater.from(mContext).inflate(R.layout.class_list_item, parent, false);
			holder.textView1 = (TextView) convertView.findViewById(R.id.title);
			holder.textView2 = (TextView) convertView.findViewById(R.id.number);

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		holder.textView1.setText(classIfication.className);
		int count = mDataBaseOperation.getUserInfoCount(classIfication.className);
		holder.textView2.setText("" + count);
		return convertView;
	}

	class ViewHolder {
		TextView textView1, textView2;
	}

}
